#include <linux/module.h>
#include <linux/platform_device.h>
#include <linux/mod_devicetable.h>
#include <linux/of.h>

struct device_node *mydevice_node;
int num;
u32 value_u32;
u64 value_u64;
u32 out_value[2];
const char *value_compatible;
struct property *my_property;

// 平台设备的初始化函数
static int my_platform_probe(struct platform_device *pdev)
{

    printk(KERN_INFO "my_platform_probe: Probing platform device\n");
    // 添加设备特定的操作
    // ...

    mydevice_node = of_find_node_by_name(NULL, "topeet");

    printk("my_platform_probe  mydevice_node->name  is %s\n", mydevice_node->name);

    mydevice_node = of_find_node_by_path("/topeet/myLed");

    printk("my_platform_probe  mydevice_node->111 =>name  is %s\n", mydevice_node->name);

    mydevice_node = of_get_parent(mydevice_node);

    printk("my_platform_probe  mydevice_node->2222 =>name  is %s\n", mydevice_node->name);

    // 获取子节点
    mydevice_node = of_get_next_child(mydevice_node, NULL);
    printk("  of_get_next_child  3333 node is %s\n", mydevice_node->name);

    // 使用compatible值查找节点
    mydevice_node = of_find_compatible_node(NULL, NULL, "my devicetree");
    printk("mydevice node   444 is %s\n", mydevice_node->name);

    mydevice_node = of_find_node_by_name(NULL, "myLed");

    // 获取 reg 属性的元素数量
    num = of_property_count_elems_of_size(mydevice_node, "reg", 4);
    printk("reg num is %d\n", num);
    // 读取 reg 属性的值
    of_property_read_u32_index(mydevice_node, "reg", 0, &value_u32);
    of_property_read_u64_index(mydevice_node, "reg", 0, &value_u64);
    printk("value u32 is 0x%X\n", value_u32);

    printk("value u64 is 0x%llx\n", value_u64);
    // 读取 reg 属性的变长数组
    of_property_read_variable_u32_array(mydevice_node, "reg", out_value, 1, 2);
    printk("out_value[0] is 0x%X\n", out_value[0]);
    printk("out_value[1] is 0x%X\n", out_value[1]);
    // 读取 compatible 属性的字符串值
    of_property_read_string(mydevice_node, "compatible", &value_compatible);
    printk("compatible value is %s\n", value_compatible);

    return 0;
}

// 平台设备的移除函数
static int my_platform_remove(struct platform_device *pdev)
{
    printk(KERN_INFO "my_platform_remove: Removing platform device\n");

    // 清理设备特定的操作
    // ...

    return 0;
}

static const struct of_device_id of_match_table_id[] = {
    {.compatible = "my devicetree"}, // 匹配设备树中的 compatible
    {/* 结束标志 */}};

// 定义平台驱动结构体
static struct platform_driver my_platform_driver = {
    .probe = my_platform_probe,
    .remove = my_platform_remove,
    .driver = {
        .name = "my_platform_device",
        .owner = THIS_MODULE,
        .of_match_table = of_match_table_id,
    },
};

// 模块初始化函数
static int __init my_platform_driver_init(void)
{
    int ret;

    // 注册平台驱动
    ret = platform_driver_register(&my_platform_driver);
    if (ret)
    {
        printk(KERN_ERR "Failed to register platform driver\n");
        return ret;
    }

    printk(KERN_INFO "my_platform_driver: Platform driver initialized\n");

    return 0;
}

// 模块退出函数
static void __exit my_platform_driver_exit(void)
{
    // 注销平台驱动
    platform_driver_unregister(&my_platform_driver);

    printk(KERN_INFO "my_platform_driver: Platform driver exited\n");
}

module_init(my_platform_driver_init);
module_exit(my_platform_driver_exit);

MODULE_LICENSE("GPL");
MODULE_AUTHOR("wfc");